<?php
/**
 * 第三方登录
 * author 胡仟
 * date 2013-08-15
 */
class OauthAction extends CommonAction {

    //qq登录
    function qqLogin() {
        require_once("./Home/API/qqConnectAPI.php");
        $qc = new QC();
        $qc->qq_login();
    }

    //qq登录返回
    function qqCallback() {
        require_once("./Home/API/qqConnectAPI.php");
        $qc = new QC();
        $access_token = $qc->qq_callback();
        $openid = $qc->get_openid();
        $qc = new QC($access_token, $openid);
        $user_info = $qc->get_user_info();
        $User = M('user_base_info');
        //首先判断qq用户是否已经在数据表里
        $user_data = $User->where("user_reg_type = 3 and openid = '$openid'")->find();
        if ($user_data) {
            //判断token与数据库的token是否一致，不一致说明数据库的token失效，执行更新
            if ($user_data['token'] != $access_token) {
                $condition['user_code'] = $user_data['user_code'];
                $update_data['token'] = $access_token;
                $User->where($condition)->save($update_data);
            }
            //设置登陆信息
            session(C('USER_AUTH_KEY'), $user_data['user_code']);
            session('user_email', $user_data['user_email']);
            session('user_nickname', $user_data['user_nickname']);
            session('user_province', $user_data['user_province']);
            session('user_city', $user_data['user_city']);
            session('user_industry', $user_data['user_industry']);
            session('api_logo_add', $user_data['api_logo_add']);
            session('user_logo_add', $user_data['user_logo_add']);
            session('user_gender', $user_data['user_gender']);
            session('user_birth_date', $user_data['user_birth_date']);
            session('user_integral', $user_data['user_integral']);
        } else {
            //不在用户表里，说明是第一次使用qq登录，插入用户数据
            $data = array();
            $data['user_nickname'] = $user_info['nickname']; //用户昵称
            $data['user_reg_type'] = 3;
            $data['token'] = $access_token;
            $data['openid'] = $openid;
            $data['api_logo_add'] = $user_info['figureurl_qq_1']; //qq头像40*40
            if ($user_info['gender'] == '男') {
                $data['user_gender'] = 1;
            } else {
                $data['user_gender'] = 2;
            }
            $data['user_cdatetime'] = date("Y-m-d H:i:s");
            //$data['user_status'] = 2; //第三方登录即已激活，不需要邮箱验证
            if ($user_code = $User->add($data)) {
                //设置登陆信息
                session(C('USER_AUTH_KEY'), $user_code);
                session('user_nickname', $data['user_nickname']);
                session('api_logo_add', $data['api_logo_add']);
                session('user_gender', $data['user_gender']);
            } else {
                //qq登录首次登录时，插入用户数据失败
                $this->redirect('Public/login');
            }
        }
        //session存储token和openid
        session('token', $access_token);
        session('openid', $openid);
        //跳转至首页
        $this->redirect('Index/index');
    }

    //sina微博登录
    function sinaLogin() {
        require_once("./Home/API/sina/config.php");
        require_once("./Home/API/sina/saetv2.ex.class.php");
        $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
        $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
        header("Location: $code_url");
    }

    //sina微博登录返回
    function sinaCallback() {
        require_once("./Home/API/sina/config.php");
        require_once("./Home/API/sina/saetv2.ex.class.php");
        $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = WB_CALLBACK_URL;
            try {
                $token = $o->getAccessToken('code', $keys); //token是个数组
                if (is_array($token)) {
                    $access_token = $token['access_token'];
                    $uid = $token['uid']; //新浪微博用户id
                    $url = "https://api.weibo.com/2/users/show.json?source=" . WB_AKEY . "&access_token=" . $access_token . "&uid=" . $uid;
                    $user_info_json = file_get_contents($url);
                    $user_info = json_decode($user_info_json, true);
                    //首先判断sina用户是否已经在数据表里
                    $User = M('user_base_info');
                    $user_data = $User->where("user_reg_type = 4 and openid = '$uid'")->find();
                    if ($user_data) {
                        //判断token与数据库的token是否一致，不一致说明数据库的token失效，执行更新
                        if ($user_data['token'] != $access_token) {
                            $condition['user_code'] = $user_data['user_code'];
                            $update_data['token'] = $access_token;
                            $User->where($condition)->save($update_data);
                        }
                        //设置登陆信息
                        session(C('USER_AUTH_KEY'), $user_data['user_code']);
                        session('user_email', $user_data['user_email']);
                        session('user_nickname', $user_data['user_nickname']);
                        session('user_province', $user_data['user_province']);
                        session('user_city', $user_data['user_city']);
                        session('user_industry', $user_data['user_industry']);
                        session('api_logo_add', $user_data['api_logo_add']);
                        session('user_logo_add', $user_data['user_logo_add']);
                        session('user_gender', $user_data['user_gender']);
                        session('user_birth_date', $user_data['user_birth_date']);
                        session('user_integral', $user_data['user_integral']);
                    } else {
                        //不在用户表里，说明是第一次使用sina登录，插入用户数据
                        $data = array();
                        $data['user_nickname'] = $user_info['screen_name']; //用户昵称
                        $data['user_reg_type'] = 4;
                        $data['token'] = $access_token;
                        $data['openid'] = $uid;
                        $data['api_logo_add'] = $user_info['profile_image_url']; //sina头像50*50
                        if ($user_info['gender'] == 'm') {
                            $data['user_gender'] = 1;
                        } else {
                            $data['user_gender'] = 2;
                        }
                        $data['user_cdatetime'] = date("Y-m-d H:i:s");
                        //$data['user_status'] = 2; //第三方登录即已激活，不需要邮箱验证
                        if ($user_code = $User->add($data)) {
                            //设置登陆信息
                            session(C('USER_AUTH_KEY'), $user_code);
                            session('user_nickname', $data['user_nickname']);
                            session('api_logo_add', $data['api_logo_add']);
                            session('user_gender', $data['user_gender']);
                        } else {
                            //sina登录首次登录时，插入用户数据失败
                            $this->redirect('Public/login');
                        }
                    }
                    //session存储token和openid
                    session('token', $access_token);
                    session('openid', $uid);
                    //跳转至首页
                    $this->redirect('Index/index');
                } else {
                    $this->redirect('Public/login');
                }
            } catch (OAuthException $e) {
                //登录失败，跳转至登录页
                $this->redirect('Public/login');
            }
        }
    }

}